<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addfn(1,'0')">添加权限</el-button>
        </div>
        <el-table
          :data="list"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          border
          default-expand-all
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column
            prop="name"
            label="名称"
            sortable
            width="180"
          />
          <el-table-column
            prop="code"
            label="标识"
            sortable
            width="180"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button size="small" type="text" @click="addToo(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="UpdataList(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delfn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <AddPremiss ref="adsPress" :show-dialog.sync="showDialog" />
  </div>
</template>

<script>
import AddPremiss from './add-permiss/index.vue'
import { getUserInfoAPP, deleteAPIList } from '@/api/permission'
import { transList } from '@/utils/index'
export default {
  name: 'Permission',
  components: {
    AddPremiss
  },
  data() {
    return {
      list: [],
      showDialog: false
    }
  },
  created() {
    this.userList()
  },
  methods: {
    async userList() {
      const res = await getUserInfoAPP()
      console.log(res)
      this.list = transList(res.data, '0')
    },
    delfn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteAPIList(id)
        console.log(res)
        this.$message.success('删除成功')
        this.userList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 添加弹出层
     */
    addfn(type, id) {
      this.showDialog = true
      this.$refs.adsPress.formData.type = type
      this.$refs.adsPress.formData.pid = id
    },
    /**
     * 添加权限点
     */
    addToo(type, id) {
      this.showDialog = true
      this.$refs.adsPress.formData.type = type
      this.$refs.adsPress.formData.pid = id
    },
    /**
     * 查看详情
     */
    UpdataList(id) {
      this.showDialog = true
      this.$refs.adsPress.getPermissionDetail(id)
    }
  }
}
</script>
